package com.sky.controller.admin;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderStatisticsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;

/**
 * @author 符健清
 * @version 1.0
 * 订单 控制器
 */
@SuppressWarnings({"all"})
@Slf4j
@Api( tags = "订单管理接口")
@RestController
@RequestMapping("/admin/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 订单分页查询
     */
    @ApiOperation("订单分页条件查询")
    @GetMapping("/conditionSearch")
    public  Result   pageSelectOrder(@RequestParam (defaultValue = "1")  Integer  page   , @RequestParam (defaultValue = "10") Integer pageSize
                                                     , LocalDateTime  beginTime , LocalDateTime endTime , String number  , String  phone , Integer  status ) {

//       将参数 使用 OrderPageQueryDTO  封装
        OrdersPageQueryDTO ordersPageQueryDTO = new OrdersPageQueryDTO();
        ordersPageQueryDTO.setBeginTime(beginTime);
        ordersPageQueryDTO.setEndTime(endTime);
        ordersPageQueryDTO.setNumber(number);
        ordersPageQueryDTO.setPhone(phone);
        ordersPageQueryDTO.setStatus(status);
        ordersPageQueryDTO.setPageSize(pageSize);
        ordersPageQueryDTO.setPage(page);
        log.info(" OderPageQueryDto :{}" , ordersPageQueryDTO);
        PageResult  pageResult =orderService.pageSelectOrder(ordersPageQueryDTO);
        Result<PageResult > result = new Result<>();
        return  result.success(pageResult);

    }
    /**
     * 取消订单
     */
    @ApiOperation("取消订单")
    @PutMapping("cancel")
    public Result cancelOrder(@RequestBody OrdersCancelDTO  ordersCancelDTO){
         log.info(" OderCancelDto :{}" , ordersCancelDTO);
         orderService.cancelOrder(ordersCancelDTO);
        Result<Object> result = new Result<>();
        return  result.success();
    }

    /**
     * 各个状态的订单数量统计
     */
    @ApiOperation("各个状态的订单数量统计")
    @GetMapping("/statistics")
    public Result  statistics(){
        OrderStatisticsVO orderStatisticsVO = new OrderStatisticsVO();

//        待接单数量
        Integer countTOBeConfirmed =orderService.counToBeConfirmed();
//        待派送数量
        Integer  countConfirmed =orderService.countConfirmed();
//        待派送中的数量
        Integer deliveryLiProgress =orderService.deliveryLiProgress();
//      封装
        orderStatisticsVO.setToBeConfirmed(countTOBeConfirmed);
        orderStatisticsVO.setConfirmed(countConfirmed);
        orderStatisticsVO.setDeliveryInProgress(deliveryLiProgress);
        Result<Object> result = new Result<>();
        return  result.success(orderStatisticsVO);
    }

    /**
     * 完成订单
     *
     */
    @ApiOperation("完成订单")
    @PutMapping("/complete/{id}")
    public  Result  finishOrder(@PathVariable Integer  id){
        log.info("完成的订单id:{}" ,id);
        orderService.finishOrder(id);
        Result<Object> result = new Result<>();
        return   result.success();
    }

    /**
     * 拒单
     */
    @ApiOperation("拒单")
    @PutMapping("/rejection")
    public Result  rejectionOrder(@RequestBody OrdersRejectionDTO ordersRejectionDTO){
        log.info("ordersRejectionDTO:{}" ,ordersRejectionDTO);
        orderService.rejectionOrder(ordersRejectionDTO);
        Result<Object> result = new Result<>();
        return   result.success();
    }

/**
 *  接单
 */
 @ApiOperation("接单")
    @PutMapping("/confirm")
    public Result  confirmOrder(@RequestBody OrdersConfirmDTO ordersConfirmDTO){
      log.info("ordersConfirmDTO:{}" ,ordersConfirmDTO);
      orderService.confirmOrder(ordersConfirmDTO.getId());
     Result<Object> result = new Result<>();
     return   result.success();

 }

    /**
     * 订单查询
     */
    @ApiOperation(" id 查询订单")
    @GetMapping("/details/{id}")
    public Result  selectOrderById(@PathVariable Integer id){
        log.info("id:{}" ,id);
        orderService.selectOrderById(id);
        Result<Object> result = new Result<>();
        return   result.success();
    }

    /**
     * 派送订单
     */
    @ApiOperation("派送订单")
    @PutMapping("/delivery/{id}")
    public Result sendOrder(@PathVariable Integer id){
        log.info("id:{}" ,id);
        orderService.sendOrder(id);
        Result<Object> result = new Result<>();
        return   result.success();
    }

}
